/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game;

import authenticate.Player;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Light
 */
public class ConverterClient extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @PersistenceUnit
    EntityManagerFactory emf;
    @PersistenceContext
    private EntityManager em;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        // response.sendRedirect("gameList.xhtml");
        PrintWriter out = response.getWriter();
        try {

            List<Player> list = em.createQuery("SELECT username FROM Player username").getResultList();
            out.println("LIST OF ALL RESISTERED PLAYERS");
            out.println("<table border=1 align=\"center\" bgcolor=\"white\" ><tr><th>FirstName</th><th>LastName</th><th>UserName</th></tr>");
            for (Player player : list) {
                out.println("<tr><td>" + player.getFirstname()
                        + "</td><td>" + player.getLastname()
                        + "</td><td>" + player.getUsername()
                        + "</td></tr>");
            }
            out.println("</table>");

            /*out.println("<html>");
            out.println("<head>");
            out.println("<title>"+request.getAttributeNames().toString() + "</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ConverterClient at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");*/

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
